home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS03.ADF / Xref / getword.c < prev    next >
C/C++ Source or Header  |  1986-04-02  |  2KB  |  70 lines

  1. /**************************************************************************
  2.  
  3. NAME
  4.  
  5.    getword -- gets next word from input
  6.  
  7. SYNOPSIS
  8.  
  9.    l = getword(w,lim);
  10.    int l;              type of object
  11.    char *w;            pointer to beginning of string returned
  12.    int lim;            maximum number of characters to be returned
  13.  
  14. DESCRIPTION
  15.  
  16.    'word' is defined as a string of letters and digits, starting with
  17.    a letter, with a maximum of lim characters
  18.  
  19. RETURNS
  20.  
  21.    l = LETTER if a word is returned
  22.      = EOF if end of file encountered
  23.      = character itself if it is not a-z or A-Z
  24.  
  25. CAUTIONS
  26.  
  27.    LETTER and DIGIT must be equally defined in calling routine.
  28.  
  29.  
  30. AUTHOR    Philip T. Ansteth
  31. DATE      Dec. 6, 1985
  32. CLIENT    ANSTETH RESEARCH
  33.  
  34. CALLED FUNCTIONS
  35.  
  36.    type -- returns type of character: letter or digit
  37.  
  38. **************************************************************************/
  39.  
  40. #define  LETTER 'a'
  41. #define  DIGIT  '0'
  42. getword(w,lim)     /* get next word from input */
  43. char *w;
  44. int lim;
  45. {
  46.    int c,t;
  47.  
  48. /*   printf("Entry to getword: lim = %d\n",lim);    */
  49.  
  50.    if(type(c = *w++ = getch()) != LETTER) { /* is first character non-alpha? */
  51.       *w = '\0';  /* second character of w is string terminator */
  52.  
  53. /*      printf("Exit from getword: c = %c\n",c);  */
  54.  
  55.       return(c);  /* return character in c too */
  56.    }
  57.    while (--lim > 0) {     /* check subsequent characters up to lim */
  58.       t = type(c = *w++ = getch());  /* put alpha or num. in w and t */
  59.       if (t != LETTER && t != DIGIT)  {
  60.          ungetch(c);
  61.          break;
  62.       }
  63.    }
  64.    *(w-1) = '\0';  /* reached end, so back up and put in string delimiter */
  65.  
  66.   /*  printf("Exit from getword: LETTER = %c\n",LETTER);  */
  67.  
  68.    return(LETTER);
  69. }
  70.